using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CPGVertragsverwaltung.Viewer
{
    public partial class MiniObjectGroup : UserControl
    {
        private MiniObjectItemCollection mItems=new MiniObjectItemCollection();
        private String mKey;

        public MiniObjectGroup()
        {
            InitializeComponent();
        }

        #region Events
        public delegate void ItemSelectedDelegate(MiniObjectItem pItem);
        public event ItemSelectedDelegate ItemSelected;
        #endregion

        #region Property
        public MiniObjectItemCollection Items
        {
            set { mItems = value; }
            get { return mItems; }
        }

        public String GroupName
        {
            set { lblGroupName.Text = value; }
            get { return lblGroupName.Text; }
        }

        public String Key
        {
            set { mKey = value; }
            get { return mKey; }
        }

        #endregion

        #region private functions
        /// <summary>
        /// Lookup for Index of Item contains Key
        /// </summary>
        /// <param name="pKey">Key</param>
        /// <returns>-1 if not found</returns>
        private int getIndexFromKey(String pKey)
        {
            int result = -1;
            foreach (MiniObjectItem item in mItems)
            {
                if (item.Key == pKey)
                {
                    result = mItems.IndexOf(item);
                }
            }
            return result;
        }

        /// <summary>
        /// resize lblGroupName and Items
        /// </summary>
        private void MiniObjectGroup_Resize(object sender, EventArgs e)
        {
            redrawItems();
        }

        /// <summary>
        /// resize Form and Items
        /// </summary>
        private void redrawItems()
        {
            this.Height = 30 + (mItems.Count * 50);

            lblGroupName.Left = 20;
            lblGroupName.Top = 0;
            lblGroupName.Width = this.Width - lblGroupName.Left;

            //resize all items
            if (mItems != null)
            {
                for (int i = 0; i < mItems.Count; i++)
                {
                    MiniObjectItem item = mItems[i];
                    item.Top = 30 + (i * 50);
                    item.Left = 0;
                    item.Width = this.Width;
                }
            }
        }

        /// <summary>
        /// Get or Set Selected Item Collection
        /// </summary>
        public MiniObjectItemCollection SelectedItems
        {
            get
            {
                MiniObjectItemCollection selectedItems = new MiniObjectItemCollection();
                foreach (MiniObjectItem item in mItems)
                {
                    if (item.IsSelected)
                    {
                        selectedItems.Add(item);
                    }
                }
                return selectedItems;
            }
            set
            {
                foreach(MiniObjectItem item in value)
                {
                    item.select();
                }
            }
        }

        #endregion
   

        /// <summary>
        /// add new item to
        /// </summary>
        /// <param name="pItem"></param>
        public void addItem(MiniObjectItem pItem)
        {
            //Add Event
            pItem.Selected += new MiniObjectItem.SeletedDelegate(OnItemSelected);

            mItems.Add(pItem);
            this.Controls.Add(pItem);
            this.redrawItems();
        }

        /// <summary>
        /// Delete Item contains Key
        /// </summary>
        /// <param name="pKey">Key</param>
        public void deleteItem(String pKey)
        {
            int Index = getIndexFromKey(pKey);
            deleteItem(Index);
        }

        /// <summary>
        /// Delete Item at Index
        /// </summary>
        /// <param name="pIndex">Index</param>
        public void deleteItem(int pIndex)
        {
            mItems[pIndex].Dispose();
            mItems.RemoveAt(pIndex);
            this.redrawItems();

        }

        /// <summary>
        /// Delete Item by MiniObjectItem
        /// </summary>
        /// <param name="pItem">Item</param>
        public void deleteItem(MiniObjectItem pItem)
        {
            int Index = mItems.IndexOf(pItem);
            deleteItem(Index);
        }

        /// <summary>
        /// raise event when a Item is selected
        /// </summary>
        /// <param name="pItem"></param>
        private void OnItemSelected(MiniObjectItem pItem)
        {
            if (this.ItemSelected != null)
            {
                this.ItemSelected(pItem);
            }
        }

        /// <summary>
        /// Deselect all Items
        /// </summary>
        public void deselectAllItems()
        {
            deselectItemsExcept(null);
        }

        /// <summary>
        /// Deselect All Items Except the given Item
        /// </summary>
        /// <param name="pItem">Item which won't be deselected</param>
        public void deselectItemsExcept(MiniObjectItem pItem)
        {
            foreach (MiniObjectItem item in mItems)
            {
                if (item!=pItem)
                {
                    item.deselect();
                }
            }
        }

    }
}
